#!/bin/env python
#-*-coding:utf-8-*-   



import time
import datetime 
import os
import sys


class Date(): 
    def getYesterday(self):   
        today=datetime.date.today()   
        oneday=datetime.timedelta(days=1)   
        yesterday=today-oneday    
        return yesterday   
          
    def getToday(self):   
        return datetime.date.today()     
     
    #获取给定参数的前几天的日期，返回一个list   
    def getDaysByNum(self,num):   
        today=datetime.date.today()   
        oneday=datetime.timedelta(days=1)       
        li=[]        
        for i in range(0,num):   
            #今天减一天，一天一天减   
            today=today-oneday   
            #把日期转换成字符串   
            #result=datetostr(today)   
            #li.append(datetostr(today))   
            li.append(today.strftime("%Y%m%d"))   
        return li   
     
    #将字符串转换成datetime类型   
    def strtodatetime(self,datestr,format):       
        return datetime.datetime.strptime(datestr,format)   
     
    #时间转换成字符串,格式为2008-08-02   
    def datetostr(self,date):     
        return   str(date)[0:10]   
    
    def date2str(self,date,format):
        return date.strftime(format)
     
    #两个日期相隔多少天，例：2008-10-03和2008-10-01是相隔两天   
    def datediff(self,beginDate,endDate):   
        format="%Y-%m-%d";   
        bd=self.strtodatetime(beginDate,format)   
        ed=self.strtodatetime(endDate,format)       
        oneday=datetime.timedelta(days=1)   
        count=0 
        while bd!=ed:   
            ed=ed-oneday   
            count+=1 
        return count   
    def datediffbydatetime(self,bd,ed):
        oneday=datetime.timedelta(days=1)   
        count=0 
        while bd < ed:   
            ed=ed-oneday   
            count+=1 
        return count   
     
    #获取两个时间段的所有时间,返回list   
    def getDays(self,beginDate,endDate):   
        format="%Y-%m-%d";   
        bd=self.strtodatetime(beginDate,format)   
        ed=self.strtodatetime(endDate,format)   
        oneday=datetime.timedelta(days=1)    
        num=self.datediff(beginDate,endDate)+1    
        li=[]   
        for i in range(0,num):    
            li.append(self.datetostr(ed))   
            ed=ed-oneday   
        return li   
     
    #获取当前年份 是一个字符串   
    def getYear(self):   
        return str(datetime.date.today())[0:4]    
     
    #获取当前月份 是一个字符串   
    def getMonth(self):   
        return str(datetime.date.today())[5:7]   
     
    #获取当前天 是一个字符串   
    def getDay(self):   
        return str(datetime.date.today())[8:10]      

    def getNow(self):   
        return datetime.datetime.now()   
    
    def timestamp2datetime(self,ts):
        return datetime.datetime.fromtimestamp(float(ts))

    def datetime2timestamp(self,dt):
        return int(time.mktime(dt.timetuple()))

    def datediffbydatetime(self,bd,ed):
        #oneday=datetime.timedelta(days=1)   
        return ed-bd
        '''count=0 
        while bd < ed:   
            ed=ed-oneday   
            count+=1 
        return count
        '''
    def timedelta(self,days):
        return datetime.timedelta(days=days)
       
def main():
    dt = Date()
    print dt.getYesterday().strftime("%Y%m%d")
    print dt.timestamp2datetime('1334637093')
    print dt.datetime2timestamp(dt.strtodatetime("2011-4-7","%Y-%m-%d"))


    pass
'''
sec="1334637093"
print time.localtime(int(sec))
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(sec)))
day = datetime.datetime(2012,1,10)
print day.timetuple()
print type(time.mktime(day.timetuple()))
print float(sec)
print datetime.datetime.fromtimestamp(float(sec))
print day
'''


if __name__ == "__main__":
    main()
        
# vim: et ts=4 sts=4 sw=4



